package ar.uba.fi.tecnicas.vista;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

public class PanelControles extends JPanel 
{
	private static final long serialVersionUID = 0;

	// Contiene los botones de las operaciones
	private JPanel botonesPanel;
	// Contiene un panel con la operacion siendo ejecutada
	private JPanel operacionPanel;
	
	private List<JButton> botones;
	private List<ActionListener> handlers;
	
	private FrameJuego frameJuego;
	
	private PanelControles self = this;
	
	public PanelControles(FrameJuego frameJuego)
	{
		super();		
		this.setSize(100, 500);
		this.setBackground(Color.GRAY);
		
		this.frameJuego = frameJuego;
		
		this.botonesPanel = new JPanel();
		this.operacionPanel = new JPanel();
		
		this.setLayout(new GridLayout(2, 1));
		
		this.inicializarComponentes();
		this.asignarEventos();
		
		this.add(this.botonesPanel);
		this.add(this.operacionPanel);
	}

	private void inicializarComponentes() 
	{
		this.botones = new ArrayList<JButton>();
		
		this.botones.add(new JButton("Comprar Terreno"));
		this.botones.add(new JButton("Ubicar Salida de Fabrica"));
		this.botones.add(new JButton("Crear Nueva Linea"));
		this.botones.add(new JButton("Comprar Maquina"));
		this.botones.add(new JButton("Elegir Primera Maquina"));
		this.botones.add(new JButton("Conectar"));
		this.botones.add(new JButton("Descongelar Tiempo"));
		this.botones.add(new JButton("Congelar Tiempo"));
		this.botones.add(new JButton("Salir"));
		
		this.botonesPanel.setLayout(new GridLayout(this.botones.size(), 1));

		for(JButton boton : this.botones)
		{
			this.botonesPanel.add(boton);
		}
	}
	
	private void asignarEventos()
	{
		this.handlers = new ArrayList<ActionListener>();
		
		/// Comprar Terreno
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelAdquirirTerreno(self)); }
			}
		);
		
		/// Ubicar Salida de Fabrica
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelUbicarSalidaFabrica(self)); }
			}
		);
		
		/// Crear Linea Nueva
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelCrearNuevaLinea(self)); }
			}
		);
		
		/// Comprar Maquina
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelComprarMaquina(self)); }
			}
		);
		
		/// Elegir Primera Maquina
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelElegirPrimeraMaquina(self)); }
			}
		);

		
		/// Conectar Maquinas
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelConectar(self)); }
			}
		);
		
		/// Descongelar Tiempo
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ frameJuego.descongelarTiempo(); }
			}
		);
		
		/// Congelar Tiempo
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ frameJuego.congelarTiempo(); }
			}
		);

		/// Salir
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ System.exit(0); }
			}
		);
		
		for(int index = 0; index < this.botones.size(); index++)
		{
			this.botones.get(index).addActionListener(this.handlers.get(index));
		}
	}

	private void cargarPanel(JPanel panel)
	{
		this.operacionPanel.removeAll();
		this.operacionPanel.add(panel, BorderLayout.SOUTH);
		this.frameJuego.pack();
	}

	public void descargarPanel()
	{
		this.operacionPanel.removeAll();
		this.frameJuego.pack();
		this.frameJuego.repaint();
	}

	public FrameJuego getFrameJuego() 
	{
		return this.frameJuego;
	}
}
